$(function(){
	//初始化
	$.post("venueCode.action",function(data){
    	$("#venueCode").append(data);
    });
	//标签页
	$("#accordion").accordion({
		autoHeight:false,
		navigation:true
	});
	//查询输入数据
	$("#submit").click(function(){
		var subBool=true;
		$("#table1 select,#table1 input").each(function(){
			if($(this).val()=="")
			{
				subBool=false;
				return;
			}
		});
		if(!subBool)
			alert("All form files are required");
		else
		{
			$("#week1 tr:gt(6)").remove();
			$("#week2 tr:gt(6)").remove();
			$("#week3 tr:gt(6)").remove();
			$("#week4 tr:gt(6)").remove();
			$("#remainders tr:gt(6)").remove();
			
			var year=$("#year").val();
			var month=$("#month").val();
			var monthDay = new Array(31,28,31,30,31,30,31,31,30,31,30,31); 
			if(year%400==0||(year%4==0&&year%100!=0)) 
			{ 
				monthDay[1]=29; 
			} 
			//获取remain天数
			var remainders=monthDay[month-1]-28;
			var weekDay=new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
			
			$("#week1 tr:eq(1) th").each(function(i){
				$(this).html(year+"-"+month+"-"+(i+1));
			});
			$("#week1 tr:eq(2) th").each(function(i){
				var date=new Date(year+"-"+month+"-"+(i+1));
				$(this).html(weekDay[date.getDay()]);
			});
			$("#week2 tr:eq(1) th").each(function(i){
				$(this).html(year+"-"+month+"-"+(i+8));
			});
			$("#week2 tr:eq(2) th").each(function(i){
				var date=new Date(year+"-"+month+"-"+(i+8));
				$(this).html(weekDay[date.getDay()]);
			});
			$("#week3 tr:eq(1) th").each(function(i){
				$(this).html(year+"-"+month+"-"+(i+15));
			});
			$("#week3 tr:eq(2) th").each(function(i){
				var date=new Date(year+"-"+month+"-"+(i+15));
				$(this).html(weekDay[date.getDay()]);
			});
			$("#week4 tr:eq(1) th").each(function(i){
				$(this).html(year+"-"+month+"-"+(i+22));
			});
			$("#week4 tr:eq(2) th").each(function(i){
				var date=new Date(year+"-"+month+"-"+(i+22));
				$(this).html(weekDay[date.getDay()]);
			});
			$("#remainders tr:eq(1) th").html("");
			$("#remainders tr:eq(2) th").html("");
			$("#remainders td").html("");
			$("#remainders tr:eq(1) th:lt("+remainders+")").each(function(i){
				$(this).html(year+"-"+month+"-"+(i+29));
			});
			$("#remainders tr:eq(2) th").each(function(i){
				if(remainders>i)
				{
					var date=new Date(year+"-"+month+"-"+(i+29));
					$(this).html(weekDay[date.getDay()]);
				}
			});

			$.post("searchSales.action",{
				month:year+"-"+month,
				remain:remainders,
				venueCode:$("#venueCode").val()
			},function(data){
				var temp=eval("("+data+")");
				$.each(temp.week1Sum,function(i,item){
					$("#week1 td:eq("+i+")").html(item.qty);
					$("#week1 td:eq("+(i+7)+")").html(item.amt);
				});
				$.each(temp.week2Sum,function(i,item){
					$("#week2 td:eq("+i+")").html(item.qty);
					$("#week2 td:eq("+(i+7)+")").html(item.amt);
				});	
				$.each(temp.week3Sum,function(i,item){
					$("#week3 td:eq("+i+")").html(item.qty);
					$("#week3 td:eq("+(i+7)+")").html(item.amt);
				});	
				$.each(temp.week4Sum,function(i,item){
					$("#week4 td:eq("+i+")").html(item.qty);
					$("#week4 td:eq("+(i+7)+")").html(item.amt);
				});	
				$.each(temp.remaindersSum,function(i,item){
					$("#remainders td:eq("+i+")").html(item.qty);
					$("#remainders td:eq("+(i+3)+")").html(item.amt);
				});	
				$("#week1").append(temp.week1SearchInfo);
				$("#week2").append(temp.week2SearchInfo);
				$("#week3").append(temp.week3SearchInfo);
				$("#week4").append(temp.week4SearchInfo);
				$("#remainders").append(temp.remainderSearchInfo);
				$("#weekSum tr:eq(2)").html("<th>Week 1<td>"+temp.week1TotalQty+"<td>"+temp.week1TotalAmt+"<td>"+(parseFloat(temp.week1TotalQty)/7).toFixed(2)+"<td>"+(parseFloat(temp.week1TotalAmt)/7).toFixed(2));
				$("#weekSum tr:eq(3)").html("<th>Week 2<td>"+temp.week2TotalQty+"<td>"+temp.week2TotalAmt+"<td>"+(parseFloat(temp.week2TotalQty)/7).toFixed(2)+"<td>"+(parseFloat(temp.week2TotalAmt)/7).toFixed(2));
				$("#weekSum tr:eq(4)").html("<th>Week 3<td>"+temp.week3TotalQty+"<td>"+temp.week3TotalAmt+"<td>"+(parseFloat(temp.week3TotalQty)/7).toFixed(2)+"<td>"+(parseFloat(temp.week3TotalAmt)/7).toFixed(2));
				$("#weekSum tr:eq(5)").html("<th>Week 4<td>"+temp.week4TotalQty+"<td>"+temp.week4TotalAmt+"<td>"+(parseFloat(temp.week4TotalQty)/7).toFixed(2)+"<td>"+(parseFloat(temp.week4TotalAmt)/7).toFixed(2));
				if(remainders!=0)
					$("#weekSum tr:eq(6)").html("<th>Remainders<td>"+temp.remaindersTotalQty+"<td>"+temp.remaindersTotalAmt+"<td>"+(parseFloat(temp.remaindersTotalQty)/remainders).toFixed(2)+"<td>"+(parseFloat(temp.remaindersTotalAmt)/remainders).toFixed(2));
				var totalAmt=(parseFloat(temp.week1TotalAmt)+parseFloat(temp.week2TotalAmt)
							+parseFloat(temp.week3TotalAmt)+parseFloat(temp.week4TotalAmt)
							+parseFloat(temp.remaindersTotalAmt)).toFixed(2);
				var totalQty=parseInt(temp.week1TotalQty)+parseInt(temp.week2TotalQty)
							+parseInt(temp.week3TotalQty)+parseInt(temp.week4TotalQty)
							+parseInt(temp.remaindersTotalQty);
				$("#totalSum tr:eq(1)").html("<th>Revenue<td>"+totalAmt+"<td>"+(totalAmt*7/parseInt(monthDay[month-1])).toFixed(2)+"<td>"+(totalAmt/parseInt(monthDay[month-1])).toFixed(2));
				$("#totalSum tr:eq(2)").html("<th>Quantity<td>"+totalQty+"<td>"+(totalQty*7/parseInt(monthDay[month-1])).toFixed(2)+"<td>"+(totalQty/parseInt(monthDay[month-1])).toFixed(2));
				
				$.each(temp.discard,function(i,item){
					$("#problem").append("<tr><td>"+item.date+"<td>"+item.item+"<td><td>"+item.qty+"<td>");
				});
			});
			
			$("#accordion").show();
		}
	});
});